package com.castle.fortress.admin.member.service.impl;

import cn.hutool.core.util.StrUtil;

import com.castle.fortress.admin.member.dto.MemberDto;
import com.castle.fortress.admin.member.entity.MemberEntity;
import com.castle.fortress.admin.utils.BizCommonUtil;
import com.castle.fortress.common.utils.ConvertUtil;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.castle.fortress.admin.member.entity.MemberGroupMembersEntity;
import com.castle.fortress.admin.member.dto.MemberGroupMembersDto;
import com.castle.fortress.admin.member.mapper.MemberGroupMembersMapper;
import com.castle.fortress.admin.member.service.MemberGroupMembersService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import java.util.List;

/**
 * 会员分组及会员关联关系表 服务实现类
 *
 * @author castle
 * @since 2023-02-13
 */
@Service
public class MemberGroupMembersServiceImpl extends ServiceImpl<MemberGroupMembersMapper, MemberGroupMembersEntity> implements MemberGroupMembersService {
    /**
    * 获取查询条件
    * @param memberGroupMembersDto
    * @return
    */
    public QueryWrapper<MemberGroupMembersEntity> getWrapper(MemberGroupMembersDto memberGroupMembersDto){
        QueryWrapper<MemberGroupMembersEntity> wrapper= new QueryWrapper();
        if(memberGroupMembersDto != null){
            MemberGroupMembersEntity memberGroupMembersEntity = ConvertUtil.transformObj(memberGroupMembersDto,MemberGroupMembersEntity.class);
            wrapper.like(memberGroupMembersEntity.getId() != null,"id",memberGroupMembersEntity.getId());
            wrapper.like(memberGroupMembersEntity.getGroupId() != null,"group_id",memberGroupMembersEntity.getGroupId());
            wrapper.like(memberGroupMembersEntity.getMemberId() != null,"member_id",memberGroupMembersEntity.getMemberId());
        }
        return wrapper;
    }


    @Override
    public IPage<MemberGroupMembersDto> pageMemberGroupMembers(Page<MemberGroupMembersDto> page, MemberGroupMembersDto memberGroupMembersDto) {
        QueryWrapper<MemberGroupMembersEntity> wrapper = getWrapper(memberGroupMembersDto);
        Page<MemberGroupMembersEntity> pageEntity = new Page(page.getCurrent(), page.getSize());
        Page<MemberGroupMembersEntity> memberGroupMembersPage=baseMapper.selectPage(pageEntity,wrapper);
        Page<MemberGroupMembersDto> pageDto = new Page(memberGroupMembersPage.getCurrent(), memberGroupMembersPage.getSize(),memberGroupMembersPage.getTotal());
        pageDto.setRecords(ConvertUtil.transformObjList(memberGroupMembersPage.getRecords(),MemberGroupMembersDto.class));
        return pageDto;
    }


    @Override
    public List<MemberGroupMembersDto> listMemberGroupMembers(MemberGroupMembersDto memberGroupMembersDto){
        List<MemberGroupMembersEntity> list = baseMapper.listMemberGroupMembers(memberGroupMembersDto);
        return ConvertUtil.transformObjList(list,MemberGroupMembersDto.class);
    }

    @Override
    public IPage<MemberDto> pageMemberGroupMembers(Page<MemberDto> page, MemberDto memberDto) {
        Map<String,Long> pageMap = BizCommonUtil.getPageParam(page);
        List<MemberEntity> memberList=baseMapper.extendsList(pageMap,memberDto);
        Long total = baseMapper.extendsCount(memberDto);
        Page<MemberDto> pageDto = new Page(page.getCurrent(), page.getSize(),total);
        pageDto.setRecords(ConvertUtil.transformObjList(memberList,MemberDto.class));
        return pageDto;
    }

    @Override
    public List<Long> distinctMemberByGroup(Long groupId) {
        return baseMapper.distinctMemberByGroup(groupId);
    }

    @Override
    public List<Long> distinctGroupByMember(Long memberId) {
        return baseMapper.distinctGroupByMember(memberId);
    }
}

